package sanrenxing.updatePwd.servlet;

import sanrenxing.updatePwd.dao.PwdUpdateDao;
import sanrenxing.updatePwd.dao.impl.PwdUpdateDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Administrator
 * @date 2020/10/14 13:53
 * 编辑者：邓志强
 * 功能：
 */
@WebServlet("/PwdUpdate")
public class PwdUpdate extends HttpServlet {
    PwdUpdateDao pwdUpdateDao=new PwdUpdateDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符集
        req.setCharacterEncoding("utf-8");
        //设置响应字符集和内容类型
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //接收来自客户端方法信息
        String method=req.getParameter("method");
        if (method.equals("teacherPwdPdt")){
            this.teacherPwdPdt(req,resp);
        }else if (method.equals("studentPwdUdt")){
            this.studentPwdUdt(req,resp);
        }
    }

    /**
     * 修改学员密码
     * @param req
     * @param resp
     */
    private void studentPwdUdt(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出流
        PrintWriter out=resp.getWriter();
        //通过session拿到学员编号
        HttpSession session=req.getSession();
        String stuId=(String) session.getAttribute("stuId");
        //拿到学员输入的旧密码
        String  stuPwd=req.getParameter("stuPwd");
        //拿到学员输入的新密码
        String newStuPwd=req.getParameter("newStuPwd");
        //调用修改学员表密码的方法，修改密码
        boolean boo=pwdUpdateDao.StudentPwdUpdate(newStuPwd,stuId,stuPwd);
        if (boo){
            out.print("ok");
        }else{
            out.print("no");
        }
    }

    /**
     * 教师修改密码
     * @param req
     * @param resp
     */
    private void teacherPwdPdt(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出流
        PrintWriter out=resp.getWriter();
        //通过session拿到账号
        HttpSession session=req.getSession();
        String userName=(String) session.getAttribute("userName");
        //拿到老师输入的旧密码
        String  teacherPwd=req.getParameter("teacherPwd");
        //拿到教师输入的新密码
        String newTeacherPwd=req.getParameter("newTeacherPwd");
        //调用修改教师表密码的方法，修改密码
        boolean boo=pwdUpdateDao.TeacherPwdUpdate(newTeacherPwd,userName,teacherPwd);
        if (boo){
            out.print("ok");
        }else{
            out.print("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
